<template>
  <div>
    <!-- 添加或修改申请使用对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="70%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="68px">
        <el-row>
          <el-col :span="24">
            <el-table :data="form.item" height="330">
              <el-table-column fixed type="index" label="序号">
              </el-table-column>
              <el-table-column prop="code" label="固定资产编号" align="center">
              </el-table-column>
              <el-table-column prop="name" label="名称" align="center">
              </el-table-column>
              <el-table-column prop="assetTypeName" label="类别" align="center">
              </el-table-column>
              <el-table-column prop="model" label="规格型号" align="center">
              </el-table-column>
            </el-table>
          </el-col>
        </el-row>
        <el-row class="mt20">
          <el-col :span="24">
            <el-form-item label="申请原因" prop="remark">
              <el-input
                type="textarea"
                v-model="form.remark"
                placeholder="请输入维修原由"
              >
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm('保存')">保存</el-button>
        <el-button type="primary" @click="submitForm('提交')">提交</el-button>
        <el-button @click="() => (this.open = false)">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { pageList as assetList } from "@/api/prod/asset/account";
import { add, get, edit } from "@/api/prod/repair/apply";

export default {
  name: "AddApply",
  data() {
    const baseForm = {
      item: [
        {
          assetId: null,
        },
      ],
      remarks: null,
    };

    return {
      // 表单类型
      type: null,
      // 是否显示弹出层
      open: false,
      // 基础表单参数
      baseForm,
      // 表单参数
      form: { ...baseForm },
      // 表单校验
      rules: {},
    };
  },

  computed: {
    // 弹出层标题
    title() {
      if (this.type !== null) return this.type + "-维修申请";
      return "";
    },
  },

  methods: {
    // 表单重置
    reset() {
      this.type = null;
      this.form = { ...this.baseForm };
    },

    /** 弹出框打开 */
    show(type, pdata, id) {
      this.reset();
      this.open = true;
      this.type = type;

      if (pdata != null && pdata.multipleSelection != null) {
        assetList({ ids: pdata.multipleSelection }).then((response) => {
          this.form.item = response.data.map((item) => {
            item.assetId = item.id;
            item.id = null;
            return item;
          });
        });
      }

      if (id != null && id != undefined) {
        get(id).then((response) => {
          this.form = response.data;
        });
      }
    },

    /** 提交按钮 */
    submitForm(action) {
      this.form.action = action;
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.type == "修改") {
            edit(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.$emit("refresh");
            });
          } else {
            add(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.$emit("refresh");
            });
          }
        }
      });
    },
  },
};
</script>

<style>
</style>